package cn.yuanqiao.web.controller.cqbc;
import cn.yuanqiao.common.core.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.core.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.cqbc.fileBackup.domain.Filebackup;
import cn.yuanqiao.cqbc.fileBackup.service.FilebackupService;
import cn.yuanqiao.cqbc.fileBackup.util.RouteUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;



/**
 * 控制器
 *
 * @author xy
 * @Date 2023-02-28 15:29:48
 */
@RestController
@RequestMapping("/cqbc/filebackup")
public class FilebackupController extends BaseController {


    @Autowired
    private FilebackupService filebackupService;

    @Resource
    private RouteUtil routeUtil;


    /**
     * 新增接口
     *
     * @author xy
     * @Date 2023-02-28
     */
    @PostMapping
    @Log(title = "新增文件备份", businessType = BusinessType.INSERT)
    public AjaxResult addItem(@RequestBody Filebackup filebackup) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        filebackup.setCreateTime(sdf.format(new Date()));
        filebackup.setCreateUser(SecurityUtils.getUserId().toString());
        return toAjax(this.filebackupService.add(filebackup));
    }

    /**
     * 修改Filebackup
     */
    @Log(title = "文件备份", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Filebackup filebackup)
    {
        return toAjax(filebackupService.updateFilebackup(filebackup));
    }
    /**
     * 修改Filebackup
     */
    @Log(title = "文件备份", businessType = BusinessType.UPDATE)
    @PutMapping("/updateState")
    public AjaxResult updateState(@RequestBody Filebackup filebackup)
    {
        return toAjax(filebackupService.updateState(filebackup));
    }

    /**
     * 删除Filebackup
     */
    @Log(title = "文件备份", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(filebackupService.deleteFilebackupByIds(ids));
    }



    /**
     * 获取Filebackup详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(filebackupService.selectFilebackupById(id));
    }

    /**
     * 查询Filebackup列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Filebackup filebackup)
    {
        startPage();
        List<Filebackup> list = filebackupService.selectFilebackupList(filebackup);
        return getDataTable(list);
    }
    /**
     * 查询列表
     *
     * @author xy
     * @Date 2023-03-01
     */
    @RequestMapping("/backup")
    public AjaxResult backup(Filebackup filebackup) {
        try {
            filebackupService.backup(filebackup);
        }catch (Exception ex){
            ex.printStackTrace();
            return AjaxResult.error("备份失败");

        }
        return AjaxResult.success();
    }


    @RequestMapping("/checkSystem")
    public AjaxResult checkProofreading() throws IOException {
        String os = System.getProperty("os.name");
        if (os != null && os.toLowerCase().startsWith("windows")) {
            return AjaxResult.success("win");
        } else if (os != null && os.toLowerCase().startsWith("linux")) {//Linux操作系统
            return AjaxResult.success("linux");
        }
        return AjaxResult.success();
    }


    /**
     * 获取路径
     * @param
     */
    @RequestMapping("/getRoute")
    public AjaxResult getRoute(HttpServletRequest request){
        String path=request.getParameter("filePath")==null?"":request.getParameter("filePath").toString();
        try {
            path = URLDecoder.decode(path, "UTF-8");
        }catch (Exception ex){
            ex.printStackTrace();
        }
        List list = routeUtil.getRoute(path);
        return AjaxResult.success("成功",list);
    }

    /**
     * 调用获取文件夹大小方法
     * @return
     */
    @RequestMapping("/getFileSize")
    public AjaxResult getFileSize(HttpServletRequest request){
        String path=request.getParameter("filePath")==null?"":request.getParameter("filePath").toString();
        String isRootPath=request.getParameter("isRootPath")==null?"":request.getParameter("isRootPath").toString();
        try {
            path = URLDecoder.decode(path, "UTF-8");
        }catch (Exception ex){
            ex.getMessage();
        }
        return AjaxResult.success(routeUtil.getFileSize(path,isRootPath));
    }

}


